<template>
  <div class="fill">
    <div class="reference"></div>
    <div class="clock" id="utility-clock" ref="utility">
      <div class="centre">
        <div class="dynamic" ref="dynamic"></div>
        <div class="expand round circle-1"></div>
        <div class="anchor hour" ref="hour">
          <div class="element thin-hand"></div>
          <div class="element fat-hand"></div>
        </div>
        <div class="anchor minute" ref="minute">
          <div class="element thin-hand"></div>
          <div class="element fat-hand minute-hand"></div>
        </div>
        <div class="anchor second" ref="second">
          <div class="element second-hand"></div>
        </div>
        <div class="expand round circle-2"></div>
        <div class="expand round circle-3"></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "clock-two",
  title: "电子表-盘",
  data() {
    return {}
  },
  mounted() {
    // 响应式
    this.autoResize(this.$refs.utility, 295 - 96)
    for (var i = 1; i <= 60; i++) this.minute(i)
    for (var i = 1; i <= 12; i++) this.hour(i)
    this.animate()
  },
  methods: {
    // 相应
    autoResize(element, nativeSize) {
      let update = function() {
        var parent = element.offsetParent
        var scale = Math.min(parent.offsetWidth, parent.offsetHeight) / nativeSize
        element.style.transform = element.style.webkitTransform = 'scale(' + scale.toFixed(3) + ')'
      }
      update()
      window.addEventListener('resize', update)
    },
    minute(n) {
      return n % 5 == 0 ? this.minuteText(n) : this.minuteLine(n)
    },
    minuteText(n) {
      let element = document.createElement('div')
      element.className = 'minute-text'
      element.innerHTML = (n < 10 ? '0' : '') + n
      this.position(element, n / 60, 135)
      this.$refs.dynamic.appendChild(element)
    },
    minuteLine(n) {
      let anchor = document.createElement('div')
      anchor.className = 'anchor'
      let element = document.createElement('div')
      element.className = 'element minute-line'
      this.rotate(anchor, n)
      anchor.appendChild(element)
      this.$refs.dynamic.appendChild(anchor)
    },
    hour(n) {
      let element = document.createElement('div')
      element.className = 'hour-text hour-' + n
      element.innerHTML = n
      this.position(element, n / 12, 105)
      this.$refs.dynamic.appendChild(element)
    },
    position(element, phase, r) {
      let theta = phase * 2 * Math.PI
      element.style.top = (-r * Math.cos(theta)).toFixed(1) + 'px'
      element.style.left = (r * Math.sin(theta)).toFixed(1) + 'px'
    },
    rotate(element, second) {
      if (element) {
        element.style.transform = element.style.webkitTransform = 'rotate(' + (second * 6) + 'deg)'
      }
    },
    animate() {
      let now = new Date()
      let time = now.getHours() * 3600 +
        now.getMinutes() * 60 +
        now.getSeconds() * 1 +
        now.getMilliseconds() / 1000
      this.rotate(this.$refs.second, time)
      this.rotate(this.$refs.minute, time / 60)
      this.rotate(this.$refs.hour, time / 60 / 12)
      requestAnimationFrame(this.animate)
    }


  },
  beforeRouteEnter(to, from, next) {
    next(vm => {})
  },
  beforeRouteLeave(to, from, next) {
    next()
  }
}
</script>

<style lang="scss">
.clock-two-bg {
    background: #000000;
    background: radial-gradient(ellipse at center, #000000 0%, #FFFFFF 50%);
    background-size: 100%;
}
.fill {
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    bottom: 0;
    // left: 0;
}
.centre {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
}

.expand {
    position: absolute;
    top: 0;
    left: 0;
    transform: translate(-50%, -50%);
}

.anchor {
    position: absolute;
    top: 0;
    left: 0;
    width: 0;
    height: 0;
}

.element {
    position: absolute;
    top: 0;
    left: 0;
}

.round {
    border-radius: 296px;
}

.circle-1 {
    background: white;
    width: 12px;
    height: 12px;
}

.circle-2 {
    background: #FA9F22;
    width: 8px;
    height: 8px;
}

.circle-3 {
    background: black;
    width: 4px;
    height: 4px;
}

.second {
    transform: rotate(180deg);
}

.minute {
    transform: rotate(54deg);
}

.second-hand {
    width: 2px;
    height: 164px;
    background: #FA9F22;
    transform: translate(-50%,-100%) translateY(24px);
}

.hour {
    transform: rotate(304.5deg);
}

.thin-hand {
    width: 4px;
    height: 50px;
    background: white;
    transform: translate(-50%,-100%);
}

.fat-hand {
    width: 10px;
    height: 57px;
    border-radius: 10px;
    background: white;
    transform: translate(-50%,-100%) translateY(-18px);
}

.minute-hand {
    height: 112px;
}

.hour-text {
    position: absolute;
    font: 40px Hei, Helvetica, Arial, sans-serif;
    color: white;
    transform: translate(-50%,-50%);
}

.hour-10 {
    padding-left: 0.4ex;
}
.hour-11 {
    padding-left: 0.25ex;
}

.minute-text {
    position: absolute;
    font: 12px Avenir Next, Helvetica, Arial, sans-serif;
    color: white;
    transform: translate(-50%,-50%);
}

.minute-line {
    background: white;
    width: 1px;
    height: 9px;
    transform: translate(-50%,-100%) translateY(-131px);
    opacity: 0.34;
}
</style>
